package linear;

public class SwapNodesPairs24 {
    public static LinkedListUtil.ListNode swapPairs(LinkedListUtil.ListNode head) {
        // we add a sentinel node so we can treat
        //the problem like a normal in place reverse
        if (head == null || head.next == null)
            return head;
        LinkedListUtil.ListNode sentinel = new LinkedListUtil.ListNode(-1);
        sentinel.next = head;

        LinkedListUtil.ListNode prev = sentinel;
        LinkedListUtil.ListNode cur = head;
        while (prev.next != null && cur.next != null) {
            LinkedListUtil.ListNode next = cur.next;
            prev.next = next;
            cur.next = next.next;
            next.next = cur;
            prev = cur;
            cur = cur.next;
        }

        return sentinel.next;
    }


    public static void main(String[] args) {
        LinkedListUtil.ListNode head = new LinkedListUtil.ListNode(1);
        LinkedListUtil.ListNode cur = head;
        for (int i = 2; i < 6; i++) {
            cur.next = new LinkedListUtil.ListNode(i);
            cur = cur.next;
        }
        LinkedListUtil.printLinkedList(swapPairs(head));
        long a = 50;
        int b = 20;
        System.out.println(Math.max(a,b));
    }
}

